package com.hscm.boss.mvc.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hscm.boss.common.consants.SysConstants;
import com.hscm.boss.common.enums.PublicStatus;
import com.hscm.boss.common.enums.ResponseCode;
import com.hscm.boss.convert.PaymentConvert;
import com.hscm.boss.dao.entity.PayWay;
import com.hscm.boss.dao.entity.UserInfo;
import com.hscm.boss.dao.result.DicEntity;
import com.hscm.boss.dao.result.DicPayTypeGroup;
import com.hscm.boss.mvc.model.AjaxResult;
import com.hscm.boss.mvc.model.from.payWay.ConfigProductWayFrom;
import com.hscm.boss.mvc.model.pages.PageInfo;
import com.hscm.boss.mvc.service.PayWayService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class PayWayController extends BaseController {

    @Resource
    private PayWayService payWayService;
    @Resource
    private PaymentConvert paymentConvert;

    @GetMapping("/payment/way/pageList")
    public AjaxResult pageList(Integer pageNo, String productCode) {
        pageNo = defPageNo(pageNo);

        IPage<PayWay> pageResult = payWayService.pageList(pageNo, SysConstants.PAGE_SIZE, productCode);

        PageInfo pageInfo = new PageInfo(pageResult.getCurrent(), pageResult.getPages());

        List<PayWay> records = pageResult.getRecords();

        HashMap<String, Object> data = new HashMap<>();
        data.put("page", pageInfo);
        data.put("list", paymentConvert.toListPayWayVo(records));

        return AjaxResult.ok(data);

    }

    /*支付方式字典接口*/
    @GetMapping("/payment/way/dicList")
    public AjaxResult payWayDicList() {
        List<DicEntity> dicPayList = payWayService.getPayWayListFromCache();
        return AjaxResult.ok(dicPayList);
    }

    /*支付类型字典接口   这个支付类型要和支付方式要一致 可以通过参数传进来实现*/
    @GetMapping("/payment/way/type/dicList")
    public AjaxResult payTypeDicList(@RequestParam("wayCode") String PayWayCode) {
        if (PayWayCode != null) {
            List<DicEntity> dicTypeList = payWayService.getPayTypeDicListFromCache(PayWayCode.trim().toUpperCase());
            return AjaxResult.ok(dicTypeList);
        }
        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }

    /*支付产品的支付方式*/
    @PostMapping("/payment/way/config")
    public AjaxResult configProductPayWay(@RequestBody @Valid ConfigProductWayFrom configProductWayFrom) {
        payWayService.configProductPayWay(configProductWayFrom);
        return AjaxResult.ok();
    }


    @PostMapping("/payment/way/remove")
    public AjaxResult removeConfigProductPayWay(@RequestParam("id") String payWayPrimary) {
        if (StrUtil.isNotBlank(payWayPrimary)) {
            payWayService.removeByPayWayPrimary(payWayPrimary);
            return AjaxResult.ok();
        }

        return AjaxResult.fail(ResponseCode.REQUEST_PARMA_ERR);
    }

}
